package com.dotstone.chipism.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;

import com.dotstone.chipism.bean.Status;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyDBHelper extends SQLiteOpenHelper {
	public static final String DB_NAME = "xintek.db";

	private SQLiteDatabase db;
	private static final String TABLE_STATUS = "status";
	private static final String CREATE_TABLE_STATUS = " create table IF NOT EXISTS " + TABLE_STATUS
			+ "  (id integer primary key autoincrement, device_id text not null,power integer,temp integer,wind integer,mode integer) ";

	private Context context;

	public MyDBHelper(Context c) {
		super(c, DB_NAME, null, 19);
		context = c;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		this.db = db;
		db.execSQL(CREATE_TABLE_STATUS);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATUS);
		db.execSQL(CREATE_TABLE_STATUS);
	}

	public void updateStatus(String id, int power, int temp, int wind, int mode) {
		if (db == null) {
			db = getWritableDatabase();
		}
		ContentValues values = new ContentValues();
		// values.put("device_id", id);
		values.put("power", power);
		values.put("temp", temp);
		values.put("wind", wind);
		values.put("mode", mode);
		db.update(TABLE_STATUS, values, "device_id=?", new String[] { id });
	}
	
	public void updateStatusPower(String id, int power){
		if (db == null) {
			db = getWritableDatabase();
		}
		ContentValues values = new ContentValues();
		values.put("power", power);
		db.update(TABLE_STATUS, values, "device_id=?", new String[] { id });
	}

	public Status getStatusById(final String id) {// 根据点击事件获取id,查询数据库
		if (db == null) {
			db = getWritableDatabase();
		}
		String[] args = { id };
		Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_STATUS + " WHERE device_id=?", args);
		if (cursor.moveToNext()) {
			Status s = new Status();
			int powerInt = cursor.getInt(cursor.getColumnIndex("power"));
			int temp = cursor.getInt(cursor.getColumnIndex("temp"));
			int wind = cursor.getInt(cursor.getColumnIndex("wind"));
			int mode = cursor.getInt(cursor.getColumnIndex("mode"));
			s.setDeviceId(id);
			s.setMode(mode);
			if (powerInt == 0) {
				s.setPower(true);
			} else {
				s.setPower(false);
			}
			s.setWind(wind);
			s.setTemp(temp);
			return s;
		}else{
			return null;
		}
	}

	public void addStatus(String id, boolean power, int temp, int wind, int mode) {
		if (db == null)
			db = getWritableDatabase();
		String[] args = { id };
		Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_STATUS + " WHERE device_id=?", args);
		if (!cursor.moveToNext()) {
			ContentValues values = new ContentValues();
			values.put("device_id", id);
			if (power) {
				values.put("power", 0);
			} else {
				values.put("power", 1);
			}
			values.put("temp", temp);
			values.put("wind", wind);
			values.put("mode", mode);
			db.insert(TABLE_STATUS, null, values);
		}
	}

	public void deleteStatus(String id) {
		if (db == null)
			db = getWritableDatabase();
		db.delete(TABLE_STATUS, "id=?", new String[] { id });
	}

}